package eval.hostEvaluate;

import java.util.List;
import java.util.Map;
import eval.host.Host;
import eval.vm.VirtualMachine;

/**
 * This class is used to chose the best host for a deployment of the given VM.
 * it gives the host a score base on the estimated unsatisfied demand 
 * after deploying the VM to this host.
 *  
 * @author Amit Peled   <amit.kapina@gmail.com>
 * @author Igal Kaufman <igal.kaufman@gmail.com>  
 */
public class SimpleEvaluation implements IHostEvaluate{

	/* (non-Javadoc)
	 * @see eval.hostEvaluate.IHostEvaluate#evaluationForHost(eval.host.Host, eval.vm.VirtualMachine, java.util.List, java.util.Map, java.util.Map)
	 */
	@Override
	public int evaluationForHost(Host host,VirtualMachine vmToBeDeployed,
			List<VirtualMachine> vmsAlreadyInHost,Map<VirtualMachine, Integer> cpuDemandOfVMs,
			Map<VirtualMachine, Integer> memDemandOfVMs) {
		//sum of minimum CPU of VM in host 
		int cpuMinCount = 0;
		int memCount = 0;
		//sum of demand CPU of VM in host at current tick
		int cpuDemandCount = 0;
		//Calculate the demand of already in host 
		//(cost / power already calculate at demand window)
		for (VirtualMachine virtualMachine : vmsAlreadyInHost) {
			cpuMinCount 	+= virtualMachine.getVMConfiguration().getMinCpu();
			cpuDemandCount  += cpuDemandOfVMs.get(virtualMachine).intValue();
			memCount += memDemandOfVMs.get(virtualMachine).intValue();
		}
		// add VM to be deployed to count
		cpuMinCount 	+= vmToBeDeployed.getVMConfiguration().getMinCpu();
		cpuDemandCount  += cpuDemandOfVMs.get(vmToBeDeployed).intValue();
		memCount += memDemandOfVMs.get(vmToBeDeployed).intValue();
		if(cpuMinCount > host.getHostCapacity().getCpuCapacity()) 
			return Integer.MIN_VALUE;
		if(memCount > host.getHostCapacity().getMemCapacity())
			return Integer.MIN_VALUE;
		return host.getHostCapacity().getCpuCapacity()- cpuDemandCount;
	}
}
